package cn.vivi.payment.controller;


import cn.vivi.commons.entity.Payment;
import cn.vivi.commons.util.Result;
import cn.vivi.payment.service.PaymentService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("payments")
@Slf4j
// 配置统一的服务降级方法
// @DefaultProperties(defaultFallback = "findPaymentFallBack")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @PostMapping
    public Result addPayment(@RequestBody Payment payment) {
        return paymentService.insert(payment) ? Result.success(): Result.error();
    }

    @GetMapping("{id}")
    // @HystrixCommand(fallbackMethod = "findPaymentFallBack")
    public Result findPayment(@PathVariable("id") String id) {
        log.info("你好{}", "世界");
        // int a = 1/ 0;
        // log.error(a + "@@@@");
        return Result.success(paymentService.findOne(id));
    }

    public Result findPaymentFallBack(@PathVariable("id") String id) {
        return Result.error("服务器出错");
    }

}
